﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/15 11:40:00
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;


namespace CommonLibrary
{
    /// <summary>
    /// 显示信息代理
    /// </summary>
    /// <param name="message"></param>
    /// <param name="color"></param>
    public delegate void ShowMessageInListViewDelegate(string message, System.Drawing.Color color);
    /// <summary>
    /// 设备变化通知代理
    /// </summary>
    public delegate void NotifyHelpersChangedDelegate();
    /// <summary>
    /// Modbus读取值变化通知代理
    /// </summary>
    /// <param name="tag"></param>
    /// <param name="datas"></param>
    public delegate void NotifyModbusHelpersRecieveDelegate(string tag, Dictionary<string, object> datas);
    /// <summary>
    /// 整体注册管理的地方，提供了一个外部统一访问的地方
    /// </summary>
    public class APPInstance
    {
        #region ------------------------------------ 不需要更改 --------------------------------------
        /// <summary>
        /// 显示信息
        /// </summary>
        private static ShowMessageDelegate SHOW_MESSAGE_DELEGATE = new ShowMessageDelegate((message, isOk) =>
                        {
                            if (AppInstanceShowMessageInListView != null)
                            {
                                try
                                {
                                    AppInstanceShowMessageInListView(message, isOk ? Color.White : Color.Red);
                                }
                                catch (Exception ex)
                                {
                                    LogHelper.Error(ex);
                                }
                            }
                        });
        /// <summary>
        /// 显示错误信息
        /// </summary>
        private static ShowErrorMessageDelegate SHOW_ERROR_MESSAGE_DELEGATE = new ShowErrorMessageDelegate((message) =>
                        {
                            try
                            {
                                if (AppInstanceShowErrorMessageInListView != null)
                                {
                                    AppInstanceShowErrorMessageInListView(message, Color.Red);
                                }
                            }
                            catch (Exception ex)
                            {
                                LogHelper.Error(ex);
                            }
                        });
        /// <summary>
        /// 设备没有数据返回，可视设备为非工作状态或异常
        /// </summary>
        private static DeviceEmptyReturnNotifyDelegate DEVICE_EMPTY_RETURN_NOTIFY_DELEGATE = new DeviceEmptyReturnNotifyDelegate((helper) =>
        {
            try
            {
                if (AppInstanceDeviceEmptyReturnNotify != null)
                {
                    AppInstanceDeviceEmptyReturnNotify(helper);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
        });
        /// <summary>
        /// PLC连接
        /// </summary>
        public static PLCHelper PLC = null;
        /// <summary>
        /// 所有设备
        /// </summary>
        public static List<BaseHelper> HELPERS = new List<BaseHelper>();
        /// <summary>
        /// 所有MODBUS
        /// </summary>
        public static List<ModbusHelper> MODBUSS = new List<ModbusHelper>(); 
        /// <summary>
        /// 事件代理用于MES信息在ListView控件上
        /// </summary>
        public static ShowMessageInListViewDelegate AppInstanceShowMESMessageInListView = null;
        /// <summary>
        /// 事件代理用于显示信息在ListView控件上
        /// </summary>
        public static ShowMessageInListViewDelegate AppInstanceShowMessageInListView = null;
        /// <summary>
        /// 事件代理用于显示错误在ListView控件上
        /// </summary>
        public static ShowMessageInListViewDelegate AppInstanceShowErrorMessageInListView = null;
        /// <summary>
        /// 设备没有数据返回，可视设备为非工作状态或异常
        /// </summary>
        public static DeviceEmptyReturnNotifyDelegate AppInstanceDeviceEmptyReturnNotify = null;
        /// <summary>
        /// 通知连接重置
        /// </summary>
        public static NotifyHelpersChangedDelegate NotifyHelpersChanged = null;
        /// <summary>
        /// Modbus通知读到数据
        /// </summary>
        public static NotifyModbusHelpersRecieveDelegate NotifyModbusHelpersRecieve = null;
        /// <summary>
        /// PLC是否连接
        /// </summary>
        /// <returns></returns>
        public static bool IsPLCConnected => PLC != null && PLC.IsConnected;

        private static TriggerHandler CreateTriggerHandler(string handlerName, string triggerAddress,
            EHandlerType handlerType, object otherAddress, EDataType triggerDataType = EDataType.SHORT,
            object triggerValue = null, object resetValue = null)
        {
            var th = new TriggerHandler()
            {
                TriggerAddress = triggerAddress,
                ResetValue = resetValue == null ? (short)0 : resetValue,
                TriggerDataType = triggerDataType,
                TriggerValue = triggerValue == null ? (short)1 : triggerValue,
                HandlerType = handlerType,
                HandlerName = handlerName,
                OtherAddress = otherAddress
            };
            BaseHelper.AddTriggerHanlder(th);
            return th;
        }

        /// <summary>
        /// 获取相关的Helper
        /// </summary>
        /// <returns></returns>
        private static List<BaseHelper> GetHelpers(EHandlerType handlerType, params ECommunicationType[] communicationTypes)
        {
            List<BaseHelper> list = null;
            if (HELPERS != null && HELPERS.Count > 0)
            {
                list = HELPERS.FindAll(v => v.TriggerHandlers != null && v.TriggerHandlers.Count > 0
                && v.TriggerHandlers.Find(m => m.HandlerType.Equals(handlerType)) != null
                && (communicationTypes == null || communicationTypes.Length == 0 || (communicationTypes.Length > 0 && communicationTypes.Contains(v.CommunicationType))));
            }
            return list;
        }
        /// <summary>
        /// 获取所有氦检仪
        /// </summary>
        /// <returns></returns>
        public static List<BaseHelper> GetAllHeliumHelper()
        {
            return GetHelpers(EHandlerType.HELIUM_HELIUM);
        }
        /// <summary>
        /// 获取Tcp类型的氦检仪
        /// </summary>
        /// <returns></returns>
        public static List<BaseHelper> GetTcpHeliumHelper()
        {
            return GetHelpers(EHandlerType.HELIUM_HELIUM, ECommunicationType.TCP);
        }
        /// <summary>
        /// 获取串口类型的氦检仪
        /// </summary>
        /// <returns></returns>
        public static List<BaseHelper> GetComHeliumHelper()
        {
            return GetHelpers(EHandlerType.HELIUM_HELIUM, ECommunicationType.SERIAL);
        }
        /// <summary>
        /// 获取所有RJ6902 HiPot
        /// </summary>
        /// <returns></returns>
        public static List<BaseHelper> GetAllRJ6902Helper()
        {
            return GetHelpers(EHandlerType.HiPot_RJ6902);
        }
        /// <summary>
        /// 获取Tcp类型RJ6902 HiPot
        /// </summary>
        /// <returns></returns>
        public static List<BaseHelper> GetTcpRJ6902Helper()
        {
            return GetHelpers(EHandlerType.HiPot_RJ6902, ECommunicationType.TCP);
        }
        /// <summary>
        /// 获取串口类型RJ6902 HiPot
        /// </summary>
        /// <returns></returns>
        public static List<BaseHelper> GetComRJ6902Helper()
        {
            return GetHelpers(EHandlerType.HiPot_RJ6902, ECommunicationType.SERIAL);
        }
        /// <summary>
        /// 获取所有扫码枪
        /// </summary>
        /// <returns></returns>
        public static List<BaseHelper> GetAllScanHelper()
        {
            return GetHelpers(EHandlerType.SCAN);
        }
        /// <summary>
        /// 获取Tcp类型扫码枪
        /// </summary>
        /// <returns></returns>
        public static List<BaseHelper> GetTcpScanHelper()
        {
            return GetHelpers(EHandlerType.SCAN, ECommunicationType.TCP);
        }
        /// <summary>
        /// 获取串口类型扫码枪
        /// </summary>
        /// <returns></returns>
        public static List<BaseHelper> GetComScanHelper()
        {
            return GetHelpers(EHandlerType.SCAN, ECommunicationType.SERIAL);
        }
        /// <summary>
        /// 获取串口类型手动扫码枪
        /// </summary>
        /// <returns></returns>
        public static List<BaseHelper> GetManualComScanHelper()
        {
            return GetHelpers(EHandlerType.SCAN, ECommunicationType.MANUAL_SERIAL);
        }
        /// <summary>
        /// 重置PLC
        /// </summary>
        public static void ResetPLC()
        {
            DisconnectPLC();
            var plcConfig = PLCHelper.GetPLCConfig();
            PLC = new PLCHelper(new PLCEntity()
            {
                ConnectTimeOut = plcConfig.ConnectTimeOut,
                IP = plcConfig.IP,
                Name = plcConfig.Name,
                OptType = plcConfig.OptType,
                Port = plcConfig.Port,
                IsStringReserve = plcConfig.IsStringReserve,
                DeviceType = plcConfig.DeviceType,
                DataFormat = plcConfig.DataFormat,
                StationNo = plcConfig.StationNo,
                UnitNo = plcConfig.UnitNo,
                IsSingle = plcConfig.IsSingle,
                IsSplitBatchRead = plcConfig.IsSplitBatchRead
            });
        }
        /// <summary>
        /// 断开PLC
        /// </summary>
        public static void DisconnectPLC()
        {
            if (PLC != null)
            {
                PLC.Disconnect();
            }
        }
        /// <summary>
        /// 断开所有设备
        /// </summary>
        public static void DisconnectHelpers()
        {
            if (HELPERS != null && HELPERS.Count > 0)
            {
                Parallel.ForEach(HELPERS, h =>
                {
                    h.Close();
                });
            }
        }
        /// <summary>
        /// 断开所有Modbus设备
        /// </summary>
        public static void DisconnectModbuses()
        {
            if (MODBUSS != null && MODBUSS.Count > 0)
            {
                Parallel.ForEach(MODBUSS, m =>
                 {
                     m.Close();
                 }
                );
            }
        }
        /// <summary>
        /// 重置Modbus
        /// </summary>
        public static void ResetModbuses()
        {
            DisconnectModbuses();
            MODBUSS.Clear();
            List<ModbusEntity> entities = ModbusHelper.GetModbusConfig();
            if (entities != null && entities.Count > 0)
            {
                Parallel.ForEach(entities, entity =>
                {
                    if (entity.TcpEntity != null && entity.CommunicationType == ECommunicationType.MODBUS_TCP)
                    {
                        var m = ModbusHelper.CreateTcpModbus(entity.Name, entity.TcpEntity.IP, entity.TcpEntity.Port, entity.Station,
                           entity.Format, entity.IsStringReserve, entity.AddressList);
                        m.ModbusNotify = (datas) =>
                        {
                            if (NotifyModbusHelpersRecieve != null)
                            {
                                NotifyModbusHelpersRecieve(entity.Name, datas);
                            }
                        };
                        MODBUSS.Add(m);
                    }
                    else if (entity.SerialEntity != null && entity.CommunicationType == ECommunicationType.MODBUS_RTU)
                    {
                        var m = ModbusHelper.CreateRtuModbus(entity.Name, entity.SerialEntity.PortName, entity.SerialEntity.BaudRate, entity.SerialEntity.DataBit,
                            entity.SerialEntity.ParityBit, entity.SerialEntity.StopBit, entity.Station, entity.Format, entity.IsStringReserve,
                            entity.AddressList);
                        m.ModbusNotify = (datas) =>
                        {
                            if (NotifyModbusHelpersRecieve != null)
                            {
                                NotifyModbusHelpersRecieve(entity.Name, datas);
                            }
                        };
                        MODBUSS.Add(m);
                    }
                }
                );
            }
        }
        /// <summary>
        /// 启动所有Modbus设备PLC监听
        /// </summary>
        public static void StartModbuses()
        {
            foreach (var m in MODBUSS)
            {
                m.Start();
            }
        }
        /// <summary>
        /// 停止所有Modbus设备PLC监听
        /// </summary>
        public static void StopModbuses()
        {
            foreach (var m in MODBUSS)
            {
                m.Stop();
            }
        }
        /// <summary>
        /// 关闭所有Modbus设备PLC监听
        /// </summary>
        public static void CloseModbuses()
        {
            foreach (var m in MODBUSS)
            {
                m.Close();
            }
        }
        /// <summary>
        /// 启动所有仪器PLC监听
        /// </summary>
        public static void StartHelpers()
        {
            foreach (var h in HELPERS)
            {
                h.Start();
            }
        }
        /// <summary>
        /// 停止所有仪器PLC监听
        /// </summary>
        public static void StopHelpers()
        {
            foreach (var h in HELPERS)
            {
                h.Stop();
            }
        }
        #endregion

        private static void CreateComHelper<T>(ComParameter comParameter, List<TriggerHandler> triggerHandlers)
        where T : BaseHelper, new()
        {
            T h = new T()
            {
                StatusAddress = comParameter.StatusAddress,
                TriggerHandlers = triggerHandlers,
                Name = comParameter.Remark,
                CommunicationType = ECommunicationType.SERIAL,
                SerialEntityData = new SerialEntity()
                {
                    PortName = comParameter.PortName
                },
                ShowMessage = SHOW_MESSAGE_DELEGATE,
                ShowErrorMessage = SHOW_ERROR_MESSAGE_DELEGATE,
                DeviceEmptyReturnNotify = DEVICE_EMPTY_RETURN_NOTIFY_DELEGATE
            };
            HELPERS.Add(h);
        }
        private static void CreateTCPHelper<T>(TcpParameter tcpParameter, List<TriggerHandler> triggerHandlers)
        where T : BaseHelper, new()
        {
            T h = new T()
            {
                StatusAddress = tcpParameter.StatusAddress,
                TriggerHandlers = triggerHandlers,
                Name = tcpParameter.Remark,
                CommunicationType = ECommunicationType.TCP,
                TcpEntityData = new TcpEntity()
                {
                    IP = tcpParameter.ServerIP,
                    Port = tcpParameter.ServerPort
                },
                ShowMessage = SHOW_MESSAGE_DELEGATE,
                ShowErrorMessage = SHOW_ERROR_MESSAGE_DELEGATE,
                DeviceEmptyReturnNotify = DEVICE_EMPTY_RETURN_NOTIFY_DELEGATE
            };
            HELPERS.Add(h);
        }

        #region  ******************** 有新设备或设备交互内容有变，请酌情调整 ************************
        /// <summary>
        /// 重置所有设备
        /// </summary>
        public static void ResetHelpers()
        {
            BaseHelper.ClearTriggerHandler();
            DisconnectHelpers();
            HELPERS.Clear();
            var otherConfig = BusinessConfig.Instance();

            #region TCP 多码扫码
            var multiScanTcpList = MultiScanHelper.GetAutoMultiScanTCPConfig();
            if (multiScanTcpList != null && multiScanTcpList.Count > 0)
            {
                foreach (var s in multiScanTcpList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    List<ScanPLCEntity> pLCEntities = new List<ScanPLCEntity>();
                    foreach (var t in s.PLCEntities)
                    {
                        pLCEntities.Add(CommonHelper.DeepCopy(t));
                    }
                    var th = CreateTriggerHandler($"触发 {s.Remark} 扫码",
                        s.TriggerAddress,
                        EHandlerType.SCAN,
                        pLCEntities);

                    triggerHandlers.Add(th);

                    CreateTCPHelper<MultiScanHelper>(s, triggerHandlers);
                }
            }
            #endregion
            #region COM 多码扫码
            var multiScanComList = MultiScanHelper.GetAutoMultiScanComConfig();
            if (multiScanComList != null && multiScanComList.Count > 0)
            {
                foreach (var s in multiScanComList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    List<ScanPLCEntity> pLCEntities = new List<ScanPLCEntity>();
                    foreach (var t in s.PLCEntities)
                    {
                        pLCEntities.Add(CommonHelper.DeepCopy(t));
                    }
                    var th = CreateTriggerHandler($"触发 {s.Remark} 扫码",
                        s.TriggerAddress,
                        EHandlerType.SCAN,
                        pLCEntities);

                    triggerHandlers.Add(th);

                    CreateComHelper<MultiScanHelper>(s, triggerHandlers);
                }
            }
            #endregion
            #region TCP 扫码
            var scanTcpList = ScanHelper.GetAutoScanTCPConfig();
            if (scanTcpList != null && scanTcpList.Count > 0)
            {
                foreach (var s in scanTcpList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    foreach (var t in s.PLCEntities)
                    {
                        var th = CreateTriggerHandler($"触发 {s.Remark}-{t.Remark} 扫码",
                           t.TriggerAddress,
                           EHandlerType.SCAN,
                           CommonHelper.DeepCopy(t));
                        triggerHandlers.Add(th);
                    }

                    CreateTCPHelper<ScanHelper>(s, triggerHandlers);
                }
            }
            #endregion
            #region COM 扫码
            var scanComList = ScanHelper.GetAutoScanComConfig();
            if (scanComList != null && scanComList.Count > 0)
            {
                foreach (var s in scanComList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    foreach (var t in s.PLCEntities)
                    {
                        var th = CreateTriggerHandler($"触发 {s.Remark}-{t.Remark} 扫码",
                           t.TriggerAddress,
                           EHandlerType.SCAN,
                           CommonHelper.DeepCopy(t));
                        triggerHandlers.Add(th);
                    }
                    CreateComHelper<ScanHelper>(s, triggerHandlers);
                }
            }
            #endregion
            #region 手动扫码
            var scanManualComList = ScanHelper.GetManualScanComConfig();
            if (scanManualComList != null && scanManualComList.Count > 0)
            {
                foreach (var s in scanManualComList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    foreach (var t in s.PLCEntities)
                    {
                        var th =
                            CreateTriggerHandler($"触发 {s.Remark}-{t.Remark} 手动扫码",
                            t.TriggerAddress,
                            EHandlerType.SCAN,
                            CommonHelper.DeepCopy(t));
                        triggerHandlers.Add(th);
                    }
                    ScanHelper h = new ScanHelper()
                    {
                        TriggerHandlers = triggerHandlers,
                        Name = s.Remark,
                        CommunicationType = ECommunicationType.MANUAL_SERIAL,
                        SerialEntityData = new SerialEntity()
                        {
                            PortName = s.PortName
                        },
                        ShowMessage = SHOW_MESSAGE_DELEGATE,
                        ShowErrorMessage = SHOW_ERROR_MESSAGE_DELEGATE,
                        DeviceEmptyReturnNotify = DEVICE_EMPTY_RETURN_NOTIFY_DELEGATE
                    };
                    HELPERS.Add(h);
                }
            }
            #endregion            
            #region COM 氦检仪
            var heliumComList = HeliumHelper.GetComHeliumConfig();
            if (heliumComList != null && heliumComList.Count > 0)
            {
                foreach (var s in heliumComList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    foreach (var sp in s.PLCEntities)
                    {
                        if (!string.IsNullOrWhiteSpace(sp.BackgroudJudgeAddress))
                        {
                            triggerHandlers.Add(
                            CreateTriggerHandler($"触发 {s.Remark}-{sp.Remark} 背景漏率检测",
                            sp.TriggerBackgroudAddress,
                            EHandlerType.HELIUM_BACKLEAKRATE,
                            new HeliumBackRatePLCEntity()
                            {
                                BackgroudJudgeAddress = sp.BackgroudJudgeAddress
                            }));
                        }
                        triggerHandlers.Add(
                            CreateTriggerHandler($"触发 {s.Remark}-{sp.Remark} 氦检",
                            sp.TriggerHeliumAddress,
                            EHandlerType.HELIUM_HELIUM,
                            new HeliumCheckPLCEntity()
                            {
                                ReadPermitAddress = sp.ReadPermitAddress,
                                HeliumJudgeAddress = sp.HeliumJudgeAddress,
                                HeliumResultAddress = sp.HeliumResultAddress,
                                HeliumTimesAddress = sp.HeliumTimesAddress,
                                LeakValueAddress = sp.LeakValueAddress,
                                PressValueAddress = sp.PressValueAddress,
                            }));
                    }
                    CreateComHelper<HeliumHelper>(s, triggerHandlers);
                }
            }
            #endregion
            #region TCP 氦检仪
            var heliumTcpList = HeliumHelper.GetTcpHeliumConfig();

            if (heliumTcpList != null && heliumTcpList.Count > 0)
            {
                foreach (var s in heliumTcpList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    foreach (var sp in s.PLCEntities)
                    {
                        if (!string.IsNullOrWhiteSpace(sp.BackgroudJudgeAddress))
                        {
                            triggerHandlers.Add(
                                CreateTriggerHandler($"触发 {s.Remark}-{sp.Remark} 背景漏率检测",
                                sp.TriggerBackgroudAddress,
                                EHandlerType.HELIUM_BACKLEAKRATE,
                                new HeliumBackRatePLCEntity()
                                {
                                    BackgroudJudgeAddress = sp.BackgroudJudgeAddress
                                }));
                        }
                        triggerHandlers.Add(
                            CreateTriggerHandler($"触发 {s.Remark}-{sp.Remark} 氦检",
                            sp.TriggerHeliumAddress,
                            EHandlerType.HELIUM_HELIUM,
                            new HeliumCheckPLCEntity()
                            {
                                ReadPermitAddress = sp.ReadPermitAddress,
                                HeliumJudgeAddress = sp.HeliumJudgeAddress,
                                HeliumResultAddress = sp.HeliumResultAddress,
                                HeliumTimesAddress = sp.HeliumTimesAddress,
                                LeakValueAddress = sp.LeakValueAddress,
                                PressValueAddress = sp.PressValueAddress,
                            }));
                    }
                    CreateTCPHelper<HeliumHelper>(s, triggerHandlers);
                }
            }
            #endregion                       
            #region TCP HIPOT
            var hipotTcpList = RJ6902Helper.GetRJ6902TcpConfig();
            if (hipotTcpList != null && hipotTcpList.Count > 0)
            {
                foreach (var s in hipotTcpList)
                {
                    List<TriggerHandler> triggerHandlers = new List<TriggerHandler>();
                    foreach (var p in s.AddressParameters)
                    {
                        triggerHandlers.Add(
                            CreateTriggerHandler($"触发 {s.Remark}-{p.Remark} 测试",
                            p.TriggerAddress,
                            EHandlerType.HiPot_RJ6902,
                            new RJ6902PLCEntity()
                            {
                                Vd1Address = p.Vd1Address,
                                Vd2Address = p.Vd2Address,
                                Vd3Address = p.Vd3Address,
                                VpAddress = p.VpAddress,
                                UptAddress = p.UptAddress,
                                RtdAddress = p.RtdAddress,
                                CapDataAddress = p.CapDataAddress,
                                OcAddress = p.OcAddress,
                                ScAddress = p.ScAddress,
                                UvAddress = p.UvAddress,
                                OvAddress = p.OvAddress,
                                Drop1Address = p.Drop1Address,
                                Drop2Address = p.Drop2Address,
                                Drop3Address = p.Drop3Address,
                                TLAddress = p.TLAddress,
                                THAddress = p.THAddress,
                                RtrAddress = p.RtrAddress,
                                CapResultAddress = p.CapResultAddress,
                                HipotResultAddress = p.ResultAddress,
                                MachineAddress = s.MachineAddress,
                                TestDoneAddress = p.TestDoneAddress,
                            }));
                    }

                    CreateTCPHelper<RJ6902Helper>(s, triggerHandlers);
                }
            }
            #endregion
            #region COM HIPOT
            var hipotComList = RJ6902Helper.GetRJ6902ComConfig();
            if (hipotComList != null && hipotComList.Count > 0)
            {
                foreach (var s in hipotComList)
                {
                    List<TriggerHandler> triggerHandlers = new List<TriggerHandler>();
                    foreach (var p in s.AddressParameters)
                    {
                        triggerHandlers.Add(
                            CreateTriggerHandler($"触发 {s.Remark}-{p.Remark} 测试",
                            p.TriggerAddress,
                            EHandlerType.HiPot_RJ6902,
                            new RJ6902PLCEntity()
                            {
                                Vd1Address = p.Vd1Address,
                                Vd2Address = p.Vd2Address,
                                Vd3Address = p.Vd3Address,
                                VpAddress = p.VpAddress,
                                UptAddress = p.UptAddress,
                                RtdAddress = p.RtdAddress,
                                CapDataAddress = p.CapDataAddress,
                                OcAddress = p.OcAddress,
                                ScAddress = p.ScAddress,
                                UvAddress = p.UvAddress,
                                OvAddress = p.OvAddress,
                                Drop1Address = p.Drop1Address,
                                Drop2Address = p.Drop2Address,
                                Drop3Address = p.Drop3Address,
                                TLAddress = p.TLAddress,
                                THAddress = p.THAddress,
                                RtrAddress = p.RtrAddress,
                                CapResultAddress = p.CapResultAddress,
                                HipotResultAddress = p.ResultAddress,
                                MachineAddress = s.MachineAddress,
                                TestDoneAddress = p.TestDoneAddress,
                            }));
                    }
                    CreateComHelper<RJ6902Helper>(s, triggerHandlers);
                }
            }
            #endregion
            #region TCP 天平
            var gagfTcpList = GagfHelper.GetAutoGagfTcpConfig();
            if (gagfTcpList != null && gagfTcpList.Count > 0)
            {
                foreach (var s in gagfTcpList)
                {
                    var triggerHandlers = new List<TriggerHandler>() {
                            CreateTriggerHandler($"触发 {s.Remark} 天平称重",
                            s.TriggerAddress,
                            EHandlerType.GAGF_GETWEIGHT,
                            new GagfWeightPLCEntity(){
                                    ResultAddress = s.ResultAddress,
                                    ResultDoneAddress = s.ResultDoneAddress,
                                }),
                           CreateTriggerHandler($"触发 {s.Remark} 天平清零",
                            s.TriggerZeroAddress,
                            EHandlerType.GAGF_REZERO,
                            new GagfRezeroPLCEntity(){
                                     ZeroDoneAddress = s.ZeroDoneAddress,
                                })
                         };

                    CreateTCPHelper<GagfHelper>(s, triggerHandlers);
                }
            }
            #endregion
            #region COM 天平
            var gagfComList = GagfHelper.GetAutoGagfComConfig();
            if (gagfComList != null && gagfComList.Count > 0)
            {
                foreach (var s in gagfComList)
                {
                    var triggerHandlers = new List<TriggerHandler>() {
                            CreateTriggerHandler($"触发 {s.Remark} 天平称重",
                            s.TriggerAddress,
                            EHandlerType.GAGF_GETWEIGHT,
                            new GagfWeightPLCEntity(){
                                    ResultAddress = s.ResultAddress,
                                    ResultDoneAddress = s.ResultDoneAddress,
                                }),
                            CreateTriggerHandler($"触发 {s.Remark} 天平清零",
                            s.TriggerZeroAddress,
                            EHandlerType.GAGF_REZERO,
                            new GagfRezeroPLCEntity(){
                                     ZeroDoneAddress = s.ZeroDoneAddress,
                                })
                         };

                    CreateComHelper<GagfHelper>(s, triggerHandlers);
                }
            }
            #endregion
            #region HIOKI 3153
            var h3153TcpList = HIOKI3153Helper.GetAutoHIOKI3153TCPConfig();
            if (h3153TcpList != null && h3153TcpList.Count > 0)
            {
                foreach (var s in h3153TcpList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    foreach (var t in s.PLCEntities)
                    {
                        triggerHandlers.Add(
                            CreateTriggerHandler($"触发 {s.Remark}-{t.Remark} 3153测量值与判定结果",
                            t.TriggerResultAddress,
                            EHandlerType.H3153_RESULT_JUDGE,
                            CommonHelper.DeepCopy(t)));
                    }
                    CreateTCPHelper<HIOKI3153Helper>(s, triggerHandlers);
                }
            }
            #endregion
            #region HIOKI 3153
            var h3153ComList = HIOKI3153Helper.GetAutoHIOKI3153ComConfig();
            if (h3153ComList != null && h3153ComList.Count > 0)
            {
                foreach (var s in h3153ComList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    foreach (var t in s.PLCEntities)
                    {
                        triggerHandlers.Add(
                            CreateTriggerHandler($"触发 {s.Remark}-{t.Remark} 3153测量值与判定结果",
                            t.TriggerResultAddress,
                            EHandlerType.H3153_RESULT_JUDGE,
                            CommonHelper.DeepCopy(t)));
                    }
                    CreateComHelper<HIOKI3153Helper>(s, triggerHandlers);
                }
            }
            #endregion
            #region HIOKI ST5520
            var st5520TcpList = HIOKIST5520Helper.GetAutoHIOKIST5520TCPConfig();
            if (st5520TcpList != null && st5520TcpList.Count > 0)
            {
                foreach (var s in st5520TcpList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    foreach (var t in s.PLCEntities)
                    {
                        triggerHandlers.Add(
                            CreateTriggerHandler($"触发 {s.Remark}-{t.Remark} 读取ST5520测量值与判定结果",
                            t.TriggerResultAddress,
                            EHandlerType.ST5520_RESULT_JUDGE,
                            CommonHelper.DeepCopy(t))
                        );
                    }
                    CreateTCPHelper<HIOKIST5520Helper>(s, triggerHandlers);
                }
            }
            #endregion
            #region HIOKI ST5520
            var st5520ComList = HIOKIST5520Helper.GetAutoHIOKIST5520ComConfig();
            if (st5520ComList != null && st5520ComList.Count > 0)
            {
                foreach (var s in st5520ComList)
                {
                    var triggerHandlers = new List<TriggerHandler>();
                    foreach (var t in s.PLCEntities)
                    {
                        var th = CreateTriggerHandler($"触发 {s.Remark}-{t.Remark} 读取ST5520测量值与判定结果",
                          t.TriggerResultAddress,
                          EHandlerType.ST5520_RESULT_JUDGE,
                          CommonHelper.DeepCopy(t));
                        triggerHandlers.Add(th);
                    }
                    CreateComHelper<HIOKIST5520Helper>(s, triggerHandlers);
                }
            }
            #endregion
        }
        #endregion
    }
}
